<?php
/**
 * Class Lib_SendSMSHook
 */

class Lib_SendSMSHook
{
    /**
     * @desc 发送业务短信
     */
    public static function businessSMS($case,$data)
    {
        $litigant= Module_Mediate_CaseLitigant::getInstance()->getAllBySearch(['telephone'], ['where' => ["caseId = {$case['id']}","mobileStatus = 1",'isDel = 0']]);
        if(empty($litigant)){
            Func_Log::write('sendMsg', 'case  mobile empty', 'res= ' . json_encode($data));
            return ['code' => 4001, 'message' => '没有可发送的手机号', 'data' => []];
        }
        $mobile = array_unique(array_column($litigant,'telephone'));
        $messageCode = $data['smsCode'];

        $mediator = Module_User_Administrator::getInstance()->getItemById($case['mediatorId'],true);
        if(empty($mediator)){
            Func_Log::write('sendMsg', 'case  mediator empty', 'res= ' . json_encode($data));
            return ['code' => 4001, 'message' => '没有负责调解员', 'data' => []];
        }
        if(empty($mediator['workerId']) && empty($mediator['workerId2'])){
            Func_Log::write('sendMsg', 'mediator workerId empty', 'res= ' . json_encode($data));
            return ['code' => 4001, 'message' => '调解员没有相应坐席', 'data' => []];
        }
        $workerId = empty($mediator['workerId']) ? $mediator['workerId2'] :$mediator['workerId'];
        $dispute = empty(Module_Mediate_Case::$disputeType[$case['disputeType']]) ? '借款合同纠纷' : Module_Mediate_Case::$disputeType[$case['disputeType']] ;
        $worker = Module_Mediate_Workernumber::getInstance()->getOne([],["workerId={$workerId}","isDel=1"],true);

        $assets = Module_InfoBase_Assets::getInstance()->getItemById($case['assetsId'],true);
        if($worker['line'] == 1){
            $worker['outNumber'] = empty($assets['outNumber']) ? $worker['outNumber'] : $assets['outNumber'];
        }else{
            $worker['outNumber'] = empty($assets['outNumber2']) ? $worker['outNumber'] : $assets['outNumber2'];
        }
        $smsSign = empty($assets['smsSign']) ? 0 : $assets['smsSign'];
        $centerName = empty($assets['smsSign']) ? CENTER_NAME : Lib_SMS_Config::$centen_name[$assets['smsCenterName']];
        if(empty($worker['outNumber'])){
            Func_Log::write('sendMsg', 'worker outNumber empty', 'res= ' . json_encode($data));
            return ['code' => 4001, 'message' => '坐席没有外显号码', 'data' => []];
        }
        $send_data = [];
        switch (true) {
            case $messageCode == 'case-accept':
                $send_data[] = $case['litigantName'];
                $send_data[] = $assets['name'];
                $send_data[] = $assets['name'];
                $send_data[] = $centerName;
                $send_data[] = $case['mediationNum'];
                $send_data[] = $worker['outNumber'];
                break;
            case $messageCode == 'case-mediate':
                $send_data[] = $case['litigantName'];
                $send_data[] = $assets['name'];
                $send_data[] = $centerName;
                $send_data[] = $case['mediatorName'];
                $send_data[] = $worker['outNumber'];
                break;
            case $messageCode == 'reduction-notice':
                $send_data[] = $case['litigantName'];
                $send_data[] = $assets['name'];
                $send_data[] = $case['mediationNum'];
                $send_data[] = $worker['outNumber'];
                $send_data[] = $assets['name'];
                $send_data[] = $assets['name'];
                break;
            case $messageCode == 'urge-contact':
                $send_data[] = $case['litigantName'];
                $send_data[] = $assets['name'];
                $send_data[] = $case['mediationNum'];
                $send_data[] = $worker['outNumber'];
                break;
            case $messageCode == 'urge-perform':
                $send_data[] = $case['litigantName'];
                $send_data[] = $assets['name'];
                $send_data[] = $case['mediationNum'];
                break;
            case $messageCode == 'close-remind' || $messageCode == 'early-close-remind':
                $send_data[] = $case['litigantName'];
                $send_data[] = $assets['name'];
                $send_data[] = $case['mediationNum'];
                $send_data[] = $assets['name'];
//                $send_data[] = date("Y年m月d日",$case['entrustExpireTime']);
                $send_data[] = $worker['outNumber'];
                break;
            case $messageCode == 'mediator-notice':
                $send_data[] = $case['litigantName'];
                $send_data[] = $assets['name'];
                $send_data[] = $assets['name'];
                $send_data[] = $centerName;
                $send_data[] = $case['mediatorName'];
                $send_data[] = $assets['name'];
                $send_data[] = $centerName;
                $send_data[] = $worker['outNumber'];
                break;
//            case $messageCode == 'mediate—success' || $messageCode == 'mediate—fail':
//                $send_data[] = $case['litigantName'];
//                $send_data[] = $assets['name'];
//                $send_data[] = $dispute;
//                $send_data[] = $case['mediationNum'];
//                $send_data[] = $assets['name'];
//                break;
//            case $messageCode == 'apply-notice' || $messageCode == 'reduction-notice':
//                $send_data[] = $case['litigantName'];
//                $send_data[] = $assets['name'];
//                $send_data[] = $assets['name'];
//                $send_data[] = $centerName;
//                $send_data[] = $worker['outNumber'];
//                break;
//            case $messageCode == 'mediate-notice':
//                $send_data[] = $case['litigantName'];
//                $send_data[] = $assets['name'];
//                $send_data[] = $assets['name'];
//                $send_data[] = $centerName;
//                $send_data[] = $worker['outNumber'];
//                $send_data[] = $assets['name'];
//                $send_data[] = $assets['name'];
//                break;
//            case $messageCode == 'search-notice':
//                $send_data[] = $case['litigantName'];
//                $send_data[] = $assets['name'];
//                $send_data[] = $case['mediationNum'];
//                $send_data[] = $worker['outNumber'];
//                $send_data[] = $centerName;
//                break;
            default:
                return ['code' => 5001, 'message' => '短信模板异常', 'data' => []];
        }
        return Lib_SMS_Main::sendSMS($mobile,$messageCode,$send_data,$case['id'],['operationUid'=>$data['operationUid'],'operationName'=>$data['operationName'],'mediationOrganId'=>$data['mediationOrganId']],$smsSign);
    }


}